package com.example.liou_liou.myapplication;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AnimationUtils;
import android.widget.TextView;

import java.util.Arrays;
import java.util.List;

public class MainActivity1 extends AppCompatActivity {
    private RecyclerView mRecyclerView;
    private List<String> mList;
    private Context mContext;
    private boolean isScrollToTop;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main1);

        mContext = this;
        mRecyclerView = (RecyclerView) findViewById(R.id.rl_list);

        String[] strings = getResources().getStringArray(R.array.numbers);
        mList = Arrays.asList(strings);

        mRecyclerView.setAdapter(new MyAdapter(mList));
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));//这里用线性显示 类似于listview
        mRecyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL_LIST));//设置分割线
        mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {//判断滑动方向
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                if (dy > 0) {
                    isScrollToTop = false;
                } else {
                    isScrollToTop = true;
                }
            }
        });

    }


    public class MyAdapter extends RecyclerView.Adapter<MyViewHolder> {
        private List<String> mList;

        public MyAdapter(List<String> mList) {
            this.mList = mList;
        }

        @Override
        public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(mContext).inflate(R.layout.item_list, parent, false);
            return new MyViewHolder(view);
        }

        @Override
        public void onBindViewHolder(MyViewHolder holder, int position) {
            holder.iv_Show.setText(mList.get(position));
            if (isScrollToTop) {//根据滑动方向设置动画
                holder.itemView.setAnimation(AnimationUtils.loadAnimation(mContext, R.anim.item_in_top_anim));
            } else {
                holder.itemView.setAnimation(AnimationUtils.loadAnimation(mContext, R.anim.item_in_bottom_anim));
            }
        }

        @Override
        public int getItemCount() {
            return mList.size();
        }

        @Override   //视图不可见时回收动画
        public void onViewDetachedFromWindow(MyViewHolder holder) {
            super.onViewDetachedFromWindow(holder);
            holder.itemView.clearAnimation();
        }
    }


    public class MyViewHolder extends RecyclerView.ViewHolder {
        private TextView iv_Show;

        public MyViewHolder(View itemView) {
            super(itemView);
            iv_Show = (TextView) itemView.findViewById(R.id.iv_show);
        }
    }

}
